//
//  WineCell.swift
//  WineComing
//
//  Created by xiaozhi on 15/6/29.
//  Copyright (c) 2015年 xiaozhi. All rights reserved.
//

import UIKit
protocol WineCellDelegate{
    func numberDidChanged(numberWithIndexPath:(Int,NSIndexPath));
}
class WineCell: UITableViewCell {
    @IBOutlet var wineImageView:XZImageView!
    @IBOutlet var wineTitle:UILabel!
    @IBOutlet var nowPriceTitle:UILabel!
    @IBOutlet var beforePriceTitle:LineLabel!
    @IBOutlet var addbutton:CoutButton!
    @IBOutlet var reduceButton:CoutButton!
    @IBOutlet var wineNumberLabel:UILabel!
    @IBOutlet var lineView:UIView!
    var minNumber:Int = 0
     var currentNumber:Int=0{
        didSet{
            wineNumberLabel.text = "\(currentNumber)"
        }
    }
    var indexPath:NSIndexPath?
    var delegate:WineCellDelegate?
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code  
        addbutton.createLayerWithPath(CoutType.add)
        nowPriceTitle.textColor = UIColor(red: 194/255.0, green: 0.0, blue: 22/255.0, alpha: 1.0)
        reduceButton.createLayerWithPath(CoutType.reduce)
        beforePriceTitle.addLine()
        wineNumberLabel.layer.borderColor = UIColor.lightGrayColor().CGColor
        wineNumberLabel.layer.borderWidth = 1.0
        wineNumberLabel.text = "\(currentNumber)"
//        contentView.backgroundColor = UIColor(red: 246/255.0, green: 246/255.0, blue: 246/255.0, alpha: 1.0)
        lineView.backgroundColor = UIColor(red: 246/255.0, green: 246/255.0, blue: 246/255.0, alpha: 1.0)
    }
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    @IBAction func addBtnClick(button:UIButton)->(){
        currentNumber+=1;
        delegate?.numberDidChanged((currentNumber,indexPath!))
    }
    @IBAction func reduceBtnClick(button:UIButton)->(){
        if currentNumber==minNumber{
            return
        }
        currentNumber-=1
        delegate?.numberDidChanged((currentNumber,indexPath!))
    }
    class func instanceFromNib()->WineCell{
        return  UINib(nibName: "WineCell", bundle: NSBundle.mainBundle()).instantiateWithOwner(nil, options: nil)[0] as! WineCell
    }
}
enum CoutType{
    case add
    case reduce
}
class CoutButton: UIButton {
    var contenLayer:CAShapeLayer!
    var type:CoutType?
    func createLayerWithPath(type:CoutType){
        self.type = type
        let path = UIBezierPath()
        let centerPoint = CGPointMake(contenLayer.bounds.size.width/2.0, contenLayer.bounds.size.height/2.0)
        let radius:CGFloat = 6
//        path.addArcWithCenter(centerPoint, radius: 10, startAngle: 0, endAngle: CGFloat(M_PI*2) , clockwise: true)
        switch type{
        case .add:
            path.moveToPoint(CGPointMake(centerPoint.x-radius, centerPoint.y))
            path.addLineToPoint(CGPointMake(centerPoint.x+radius, centerPoint.y))
            path.moveToPoint(CGPointMake(centerPoint.x, centerPoint.y-radius));
            path.addLineToPoint(CGPointMake(centerPoint.x, centerPoint.y+radius))
        case .reduce:
            path.moveToPoint(CGPointMake(centerPoint.x-radius, centerPoint.y))
            path.addLineToPoint(CGPointMake(centerPoint.x+radius, centerPoint.y))
        default:break
        }
        contenLayer.path = path.CGPath
    }
    func highLight()->(){
        let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")
        var toValue=NSValue(CATransform3D: CATransform3DIdentity)
        if let a = type{
            switch a{
            case .add:
                toValue = NSValue(CATransform3D:CATransform3DMakeScale(1.15, 1.15, 1))
            case .reduce:
                toValue = NSValue(CATransform3D:CATransform3DMakeScale(0.85, 0.85, 1))
            default:toValue = NSValue(CATransform3D:CATransform3DMakeScale(1.15, 1.15, 1))
            }
        }

        scaleAnimation.fromValue = NSValue(CATransform3D: CATransform3DIdentity)
        scaleAnimation.toValue = toValue
        scaleAnimation.duration = 0.2
        contenLayer.addAnimation(scaleAnimation, forKey: nil)
    }
    func unHighLight(sender:AnyObject){
//        contenLayer.strokeColor = UIColor.redColor().CGColor
    }
    override func awakeFromNib(){
        
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.layer.cornerRadius = 3.0
        self.layer.borderWidth = 1.0
        self.layer.borderColor = UIColor.lightGrayColor().CGColor
        let pathRect = bounds
        contenLayer = CAShapeLayer()
        contenLayer.fillColor = UIColor.clearColor().CGColor
        contenLayer.frame = pathRect
        contenLayer.strokeColor = UIColor(hex: 0x2f2f2f, alpha: 1.0).CGColor
        contenLayer.lineWidth = 1
        layer.addSublayer(contenLayer)
        self.addTarget(self, action: Selector("highLight"), forControlEvents: UIControlEvents.TouchDown)
        self.addTarget(self, action: Selector("unHighLight:"), forControlEvents: UIControlEvents.TouchUpInside)
        
    }
}